package com.bailong.administrator.ausedcar.ui.homepagefragment;

import android.os.Handler;
import android.util.Log;

import com.bailong.administrator.ausedcar.bean.HomeHotBean;
import com.bailong.administrator.ausedcar.bean.HomeLikeBean;
import com.bailong.administrator.ausedcar.bean.HomeShowBean;
import com.bailong.administrator.ausedcar.config.UrlConfig;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Administrator on 2016/9/6.
 */
public class HomePresenter implements HomeContract.Presenter {
    private HomeContract.Mode mode;
    private HomeContract.View view;

    public HomePresenter(HomeContract.View view) {
        this.view = view;
        this.mode=new HomeMode();
    }

    @Override
    public void loadHomeLikeBean() {
        HashMap<String ,String> hashMap =new HashMap<>();
        hashMap.put(UrlConfig.parmas._apikey,UrlConfig.HomeLikeValues._apikey);
        hashMap.put(UrlConfig.parmas.appver,UrlConfig.HomeLikeValues.appver);
        hashMap.put(UrlConfig.parmas.channel,UrlConfig.HomeLikeValues.channel);
        hashMap.put(UrlConfig.parmas.cityid,UrlConfig.HomeLikeValues.cityid);
        hashMap.put(UrlConfig.parmas.nb,UrlConfig.HomeLikeValues.nb);
        hashMap.put(UrlConfig.parmas.os,UrlConfig.HomeLikeValues.os);
        mode.loadHomeLikeBean(new Callback<HomeLikeBean>() {
            @Override
            public void onResponse(Call<HomeLikeBean> call, Response<HomeLikeBean> response) {
                if (response.isSuccessful()){
                    final HomeLikeBean body = response.body();
                    Handler handler=new Handler();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            view.onResponse(body);

                        }
                    });

                }
            }

            @Override
            public void onFailure(Call<HomeLikeBean> call, Throwable t) {

            }
        } ,hashMap);
    }

    @Override
    public void loadHomeHotBean() {
        HashMap hashMap=new HashMap();
        hashMap.put(UrlConfig.parmas._apikey,UrlConfig.HomeLikeValues._apikey);
        hashMap.put(UrlConfig.parmas.appver,UrlConfig.HomeLikeValues.appver);
        hashMap.put(UrlConfig.parmas.channel,UrlConfig.HomeLikeValues.channel);
        hashMap.put(UrlConfig.parmas.cityid,UrlConfig.HomeLikeValues.cityid);
        hashMap.put(UrlConfig.parmas.nb,UrlConfig.HomeLikeValues.nb);
        hashMap.put(UrlConfig.parmas.os,UrlConfig.HomeLikeValues.os);
        mode.loadHomeHotBean(new Callback<HomeHotBean>() {
            @Override
            public void onResponse(Call<HomeHotBean> call, Response<HomeHotBean> response) {
                if (response.isSuccessful()){
                    final HomeHotBean body = response.body();
                    Handler handler=new Handler();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            view.onResponse(body);

                        }
                    });

                }
            }

            @Override
            public void onFailure(Call<HomeHotBean> call, Throwable t) {

            }
        },hashMap);
    }

    @Override
    public void loadHomeShowBean() {
        Map<String,String> hashMap= new HashMap<>();
        hashMap.put(UrlConfig.parmas._apikey,UrlConfig.HomeLikeValues._apikey);
        hashMap.put(UrlConfig.parmas.appver,UrlConfig.HomeLikeValues.appver);
        hashMap.put(UrlConfig.parmas.channel,UrlConfig.HomeLikeValues.channel);
        hashMap.put(UrlConfig.parmas.cityid,UrlConfig.HomeLikeValues.cityid);
        hashMap.put(UrlConfig.parmas.nb,UrlConfig.HomeLikeValues.nb);
        hashMap.put(UrlConfig.parmas.os,UrlConfig.HomeLikeValues.os);

        mode.loadHomeShowBean(new Callback<HomeShowBean>() {
            @Override
            public void onResponse(Call<HomeShowBean> call, Response<HomeShowBean> response) {
                if (response.isSuccessful()){
                    final HomeShowBean body = response.body();
                    Handler handler=new Handler();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Log.i("jerry", "run: ===========");
                            view.onResponse(body);

                        }
                    });

                }
            }

            @Override
            public void onFailure(Call<HomeShowBean> call, Throwable t) {

            }
        },hashMap);
    }
}
